﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#include "ComponentEditorUrlConfig.h"
#include "pugixml.h"
#include "FilePath.h"
#include "GbmpFileSystem.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace gfam;

const std::wstring& XML_ROOT = L"root";
const std::wstring& XML_PRODUCTION = L"production";
const std::wstring& XML_DEV = L"dev";
const std::wstring& XML_COMMON = L"common";
const std::wstring& XML_BANNER = L"banner";
const std::wstring& XML_FEEDBACK = L"feedback";

UrlConfig* UrlConfig::Get()
{
    static UrlConfig s_Library;
    return &s_Library;
}

UrlConfig::UrlConfig()
{
}

UrlConfig::~UrlConfig()
{
}

bool UrlConfig::LoadUrlConfig(const std::wstring& fileName)
{
    std::wstring urlDir = FileSystem::GetExeDirPath();
    std::wstring urlFile = FilePath::Combine(urlDir, fileName);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(IsFileExist(urlFile), urlFile + L"不存在", L"paramodel", L"2024-02-20");

    pugi::xml_document xmlDoc;
    pugi::xml_parse_result result = xmlDoc.load_file(urlFile.c_str());
    bool bLoad = (pugi::status_ok == result.status);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(bLoad, urlFile + L"加载失败", L"paramodel", L"2024-02-20");

    std::wstring envValue = XML_DEV;

    std::wstring nodePath = XML_ROOT + L"/" + envValue;
    pugi::xpath_node urlNode = xmlDoc.select_node(nodePath.c_str());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(urlNode, L"url节点加载失败", L"paramodel", L"2024-02-20");

    std::wstring bannerUrl = urlNode.node().attribute(XML_BANNER.c_str()).as_string();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(!bannerUrl.empty(), L"bannerUrl获取失败", L"paramodel", L"2024-02-20");

    m_BannerUrl = StringUtil::ToString(bannerUrl);

    std::wstring feedBackUrl = urlNode.node().attribute(XML_FEEDBACK.c_str()).as_string();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(!feedBackUrl.empty(), L"feedbackUrl获取失败", L"paramodel", L"2024-02-20");

    m_FeedBackUrl = StringUtil::ToString(feedBackUrl);

    return true;
}

bool UrlConfig::IsFileExist(const std::wstring& filePath)
{
    if (filePath.empty())
        return false;

    FileInfo fi(filePath);
    return fi.Exists();
}

std::string UrlConfig::GetBannerUrl() const
{
    return m_BannerUrl;
}

std::string UrlConfig::GetFeedBackUrl() const
{
    return m_FeedBackUrl;
}
